package com.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.admin.common.constant.UserConstants;
import com.cloud.admin.common.utils.StringUtils;
import com.cloud.admin.mapper.SysPostMapper;
import com.cloud.admin.model.entity.SysPost;
import com.cloud.admin.service.SysPostService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位信息表
 *
 * @author douhaichao code generator
 * @date 2019-07-09 10:27:07
 */
@Service
@AllArgsConstructor
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public SysPost selectOne(SysPost req) {
        QueryWrapper queryWrapper = new QueryWrapper<SysPost>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SysPost> selectPostsByUserId(Long userId) {
        List<SysPost> userPosts = baseMapper.selectPostsByUserId(userId);
        List<SysPost> posts = baseMapper.selectList(null);
        for (SysPost post : posts) {
            for (SysPost userRole : userPosts) {
                if (post.getPostId().longValue() == userRole.getPostId().longValue()) {
                    post.setFlag(true);
                    break;
                }
            }
        }
        return posts;
    }

    @Override
    public String checkPostNameUnique(SysPost post) {
        Integer postId = StringUtils.isNull(post.getPostId()) ? -1 : post.getPostId();
        SysPost info = baseMapper.selectPostNameUnique(post.getPostName());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) {
            return UserConstants.POST_NAME_NOT_UNIQUE;
        }
        return UserConstants.POST_NAME_UNIQUE;
    }

    @Override
    public String checkPostCodeUnique(SysPost post) {
        Integer postId = StringUtils.isNull(post.getPostId()) ? -1 : post.getPostId();
        SysPost info = baseMapper.selectPostCodeUnique(post.getPostCode());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) {
            return UserConstants.POST_CODE_NOT_UNIQUE;
        }
        return UserConstants.POST_CODE_UNIQUE;
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<SysPost> selectAll(SysPost req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<SysPost> selectPage(Page page, SysPost req) {
        IPage<SysPost> list = baseMapper.selectPage(
                page,
                new QueryWrapper<SysPost>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<SysPost> selectPageByLike(Page page, SysPost req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getPostId()) {
            queryWrapper.eq("post_id", req.getPostId());
        }

        if (!StrUtil.isEmpty(req.getPostCode())) {
            queryWrapper.like("post_code", req.getPostCode());
        }

        if (!StrUtil.isEmpty(req.getPostName())) {
            queryWrapper.like("post_name", req.getPostName());
        }

        if (null != req.getPostSort()) {
            queryWrapper.eq("post_sort", req.getPostSort());
        }

        if (!StrUtil.isEmpty(req.getStatus())) {
            queryWrapper.like("status", req.getStatus());
        }

        if (!StrUtil.isEmpty(req.getCreateBy())) {
            queryWrapper.like("create_by", req.getCreateBy());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (!StrUtil.isEmpty(req.getUpdateBy())) {
            queryWrapper.like("update_by", req.getUpdateBy());
        }

        if (null != req.getUpdateTime()) {
            queryWrapper.eq("update_time", req.getUpdateTime());
        }

        if (!StrUtil.isEmpty(req.getRemark())) {
            queryWrapper.like("remark", req.getRemark());
        }
        IPage<SysPost> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(SysPost req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }
}
